/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.generator;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import jFamilyTree.Messages;
import jFamilyTree.generator.model.ModelUtils;

/**
 * Image list dialog
 * 
 * @author Bernard Bou
 */
public class ImageListDialog extends ReferenceListDialog
{
	private static final long serialVersionUID = 1L;

	/**
	 * Image repository
	 */
	private URL theImageRepository;

	// C O N S T R U C T O R

	/**
	 * Constructor
	 */
	public ImageListDialog(Controller thisController)
	{
		super(thisController);
		setTitle(Messages.getString("ImageListDialog.0")); //$NON-NLS-1$
		theLabel.setText(Messages.getString("ImageListDialog.1")); //$NON-NLS-1$
		theReferenceTable.setRowHeight(32);
		theScrollPane.setPreferredSize(new Dimension(300, 320));

		JButton thisCheckMissingButton = new JButton(Messages.getString("ImageListDialog.2")); //$NON-NLS-1$
		thisCheckMissingButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent e)
			{
				checkMissing();
			}
		});
		JButton thisCheckUnusedButton = new JButton(Messages.getString("ImageListDialog.3")); //$NON-NLS-1$
		thisCheckUnusedButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent e)
			{
				checkUnused();
			}
		});
		theButtonPanel.add(thisCheckMissingButton, null);
		theButtonPanel.add(thisCheckUnusedButton, null);
	}

	// O V E R R I D E S

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.generator.ReferenceListDialog#update()
	 */
	@Override
	protected void update()
	{
		theLabel.setText(Messages.getString("ImageListDialog.1")); //$NON-NLS-1$
		theImageRepository = theController.makeImageRepositoryURL();
		Map<String, SortedSet<String>> thisImageToLocationMap = ModelUtils.getImageMap(theController.getModel());
		setModel(thisImageToLocationMap);
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.generator.ReferenceListDialog#setModel(java.util.Map)
	 */
	@Override
	protected void setModel(Map<String, SortedSet<String>> thisTargetToLocationMap)
	{
		super.setModel(thisTargetToLocationMap);

		// renderer
		TableColumn thisImageColumn = theReferenceTable.getColumnModel().getColumn(0);
		thisImageColumn.setWidth(150);
		thisImageColumn.setCellRenderer(new DefaultTableCellRenderer()
		{
			private static final long serialVersionUID = 1L;

			/*
			 * (non-Javadoc)
			 * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
			 */
			@Override
			public Component getTableCellRendererComponent(JTable thisTable, Object thisValue, boolean thisIsSelected, boolean thisHasFocus, int thisRow, int thisColumn)
			{
				ParameterModel.Entry thisEntry = (ParameterModel.Entry) thisValue;
				String thisImageFile = thisEntry.theKey;
				setIcon(makeIcon(thisImageFile));
				setText(thisImageFile);
				return this;
			}
		});

		TableColumn thisLocationColumn = theReferenceTable.getColumnModel().getColumn(1);
		thisLocationColumn.setCellRenderer(new DefaultTableCellRenderer()
		{
			private static final long serialVersionUID = 1L;

			/*
			 * (non-Javadoc)
			 * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
			 */
			@Override
			public Component getTableCellRendererComponent(JTable thisTable, Object thisValue, boolean thisIsSelected, boolean thisHasFocus, int thisRow, int thisColumn)
			{
				ParameterModel.Entry thisEntry = (ParameterModel.Entry) thisValue;
				String thisLocation = thisEntry.theValue;
				if (thisLocation.startsWith("default") || thisLocation.equals("background")) //$NON-NLS-1$ //$NON-NLS-2$
				{
					setBackground(Color.LIGHT_GRAY);
					setForeground(Color.WHITE);
				}
				else if (thisLocation.startsWith("node")) //$NON-NLS-1$
				{
					setBackground(Color.WHITE);
					setForeground(Color.BLUE);
				}
				else if (thisLocation.startsWith("treeedge") || thisLocation.startsWith("edge")) //$NON-NLS-1$ //$NON-NLS-2$
				{
					setBackground(Color.WHITE);
					setForeground(Color.BLACK);
				}
				else if (thisLocation.startsWith("unused")) //$NON-NLS-1$
				{
					setBackground(Color.WHITE);
					setForeground(Color.BLACK);
				}
				else
				{
					setBackground(Color.RED);
					setForeground(Color.WHITE);
				}

				setText(thisLocation);
				return this;
			}
		});
	}

	// H E L P E R S

	/**
	 * Check missing files
	 */
	private void checkMissing()
	{
		theImageRepository = theController.makeImageRepositoryURL();
		Map<String, SortedSet<String>> thisImageToLocationMap = ModelUtils.getImageMap(theController.getModel());
		Set<String> theseImages = thisImageToLocationMap.keySet();

		if (theImageRepository.getProtocol().equals("file")) //$NON-NLS-1$
		{
			try
			{
				URI thisUri = theImageRepository.toURI();
				File thisFile = new File(thisUri);
				if (thisFile.isDirectory())
				{
					for (File thisDirectoryEntry : thisFile.listFiles())
					{
						String thisName = thisDirectoryEntry.getName();
						if (theseImages.contains(thisName))
						{
							thisImageToLocationMap.remove(thisName);
						}
					}
					setModel(thisImageToLocationMap);
					theLabel.setText(Messages.getString("ImageListDialog.4") + thisUri.toString()); //$NON-NLS-1$
				}
			}
			catch (URISyntaxException thisException)
			{
			}
		}
	}

	/**
	 * Check unused files
	 */
	private void checkUnused()
	{
		theImageRepository = theController.makeImageRepositoryURL();
		Map<String, SortedSet<String>> thisUnusedToLocationMap = new TreeMap<String, SortedSet<String>>();
		Map<String, SortedSet<String>> thisImageToLocationMap = ModelUtils.getImageMap(theController.getModel());
		Set<String> theseImages = thisImageToLocationMap.keySet();

		if (theImageRepository.getProtocol().equals("file")) //$NON-NLS-1$
		{
			try
			{
				URI thisUri = theImageRepository.toURI();
				File thisFile = new File(thisUri);
				if (thisFile.isDirectory())
				{
					for (File thisDirectoryEntry : thisFile.listFiles())
					{
						if (thisDirectoryEntry.isDirectory())
							continue;
						String thisName = thisDirectoryEntry.getName();
						if (!theseImages.contains(thisName))
						{
							SortedSet<String> thisValue = new TreeSet<String>();
							thisValue.add(Messages.getString("ImageListDialog.6")); //$NON-NLS-1$
							thisUnusedToLocationMap.put(thisName, thisValue);
						}
					}
					setModel(thisUnusedToLocationMap);
					theLabel.setText(Messages.getString("ImageListDialog.5") + thisUri.toString()); //$NON-NLS-1$
				}
			}
			catch (URISyntaxException thisException)
			{
			}
		}
	}

	/**
	 * Make icon
	 * 
	 * @param thisImageFile
	 *        image file
	 * @return icon
	 */
	private Icon makeIcon(String thisImageFile)
	{
		try
		{
			URL thisUrl = new URL(theImageRepository, thisImageFile);
			return new ImageIcon(thisUrl);
		}
		catch (MalformedURLException e)
		{
			return null;
		}
	}
}
